<?php
/*
 *      [Discuz! X] (C)2012-2012 bbs.heirui.cn.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: addpay.inc.php 4358 2012-07-02 20:43:24Z yanyuxiaoyao $
 */
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
$cvar= $_G['cache']['plugin']['yy_pay'];
load_yypayprosetting();
$gjsd=$_G['load_yyplugin']['yy_pay']['pro'][$_G['groupid']];
$gjsd['max']=intval($gjsd['max'])?intval($gjsd['max']):$cvar['p_max'];
function load_yypayprosetting(){
		global $_G;
		if(isset($_G['load_yyplugin']['yy_pay']['pro']))
			return;
		if($_G['setting']['version']=='X2.5')
			$dircache='sysdata';
		else
			$dircache='cache';
		if(file_exists(DISCUZ_ROOT.'./data/'.$dircache.'/cache_yy_pay_pro.php'))
			require_once DISCUZ_ROOT.'./data/'.$dircache.'/cache_yy_pay_pro.php';
		$_G['load_yyplugin']['yy_pay']['pro']=$pay_pro;
	}
?>
function addpay(cmd) {
	checkFocus();
	ShowPayEditorMenu(cmd);
	return;
}
function ShowPayEditorMenu(tag, params) {
	var sel, selection;
	var str = '', strdialog = 0, stitle = '';
	var ctrlid = editorid + (params ? '_cst' + params + '_' : '_') + tag;
	var opentag = '[<?php echo $cvar['p_code'];?>]';
	var closetag = '[/<?php echo $cvar['p_code'];?>]';
	var menu = $(ctrlid + '_menu');
	var pos = [0, 0];
	var menuwidth = 270;
	var menupos = '43!';
	var menutype = 'menu';

	if(BROWSER.ie) {
		sel = wysiwyg ? editdoc.selection.createRange() : document.selection.createRange();
		pos = getCaret();
	}
	selection = sel ? (wysiwyg ? sel.htmlText : sel.text) : getSel();
	if(selection) {
		return insertText((opentag + selection + closetag), strlen(opentag), strlen(closetag), true, sel);
	}
	stitle = '<?php echo lang('plugin/yy_pay','var5');?>';
	str='<?php echo lang('plugin/yy_pay','var7');?>:<br /><textarea id="' + ctrlid + '_param_1" style="width: 98%" cols="50" rows="5" class="txtarea"></textarea><br /><?php echo lang('plugin/yy_pay','var8');?>:</label> <input type="text" size="3" id="' + ctrlid + '_param_2" class="px pxs" /> <?php echo $_G['setting']['extcredits'][$cvar['p_credit']]['unit'].$_G['setting']['extcredits'][$cvar['p_credit']]['title'];?><br /><?php echo lang('plugin/yy_pay','var9').'<font color="#F00">'.$cvar['p_credits'].$_G['setting']['extcredits'][$cvar['p_credit']]['unit'].$_G['setting']['extcredits'][$cvar['p_credit']]['title'].'</font>'.lang('plugin/yy_pay','var28');?><br /><?php echo lang('plugin/yy_pay','var32');?><font color="#F00"><?php echo $gjsd['max'].$_G['setting']['extcredits'][$cvar['p_credit']]['unit'].$_G['setting']['extcredits'][$cvar['p_credit']]['title'];?></font>';
	menuwidth = 320;
	menupos = '00';
	//menutype = 'win';

	var menu = document.createElement('div');
	menu.id = ctrlid + '_menu';
	menu.style.display = 'none';
	menu.className = 'p_pof upf';
	menu.style.width = menuwidth + 'px';
	if(menupos == '00') {
			menu.className = 'fwinmask';
			s = '<table width="100%" cellpadding="0" cellspacing="0" class="fwin"><tr><td class="t_l"></td><td class="t_c"></td><td class="t_r"></td></tr><tr><td class="m_l">&nbsp;&nbsp;</td><td class="m_c">'
				+ '<h3 class="flb"><em>' + stitle + '</em><span><a onclick="hideMenu(\'\', \'menu\');return false;" class="flbc" href="javascript:;"><?php echo lang('plugin/yy_pay','var10');?></a></span></h3><div class="c">' + str + '</div>'
				+ '<p class="o pns"><button type="submit" id="' + ctrlid + '_submit" class="pn pnc"><strong><?php echo lang('plugin/yy_pay','var11');?></strong></button></p>'
				+ '</td><td class="m_r"></td></tr><tr><td class="b_l"></td><td class="b_c"></td><td class="b_r"></td></tr></table>';
		} else {
			s = '<div class="p_opt cl"><span class="y" style="margin:-10px -10px 0 0"><a onclick="hideMenu();return false;" class="flbc" href="javascript:;"><?php echo lang('plugin/yy_pay','var10');?></a></span><div>' + str + '</div><div class="pns mtn"><button type="submit" id="' + ctrlid + '_submit" class="pn pnc"><strong><?php echo lang('plugin/yy_pay','var11');?></strong></button></div></div>';
		}
	menu.innerHTML = s;
	$(editorid + '_editortoolbar').appendChild(menu);
	showMenu({'ctrlid':ctrlid,'mtype':menutype,'evt':'click','duration':3,'cache':0,'drag':1,'pos':menupos});

	try {
		if($(ctrlid + '_param_1')) {
			$(ctrlid + '_param_1').focus();
		}
	} catch(e) {}
	var objs = menu.getElementsByTagName('*');
	for(var i = 0; i < objs.length; i++) {
		_attachEvent(objs[i], 'keydown', function(e) {
			e = e ? e : event;
			obj = BROWSER.ie ? event.srcElement : e.target;
			if((obj.type == 'text' && e.keyCode == 13) || (obj.type == 'textarea' && e.ctrlKey && e.keyCode == 13)) {
				if($(ctrlid + '_submit') && tag != 'image') $(ctrlid + '_submit').click();
				doane(e);
			} else if(e.keyCode == 27) {
				hideMenu();
				doane(e);
			}
		});
	}
	if($(ctrlid + '_submit')) $(ctrlid + '_submit').onclick = function() {
		checkFocus();
		if(BROWSER.ie && wysiwyg) {
			setCaret(pos[0]);
		}
		var mincredits = parseInt($(ctrlid + '_param_2').value);
					if(mincredits > 0) {
						if(mincredits><?php echo $gjsd['max'];?>){
							showDialog('<?php echo lang('plugin/yy_pay','var33');?>', 'error');
							return;
						}
						opentag = '[<?php echo $cvar['p_code'];?>=';
						opentag += mincredits;
						opentag += ']';
					} else {
						opentag = '[<?php echo $cvar['p_code'];?>]';
					}
				str = $(ctrlid + '_param_1') && $(ctrlid + '_param_1').value ? $(ctrlid + '_param_1').value : (selection ? selection : '');
				if(wysiwyg) {
					str = preg_replace(['<', '>'], ['&lt;', '&gt;'], str);
					str = str.replace(/\r?\n/g, '<br />');
				}
				str = opentag + str + closetag;
				insertText(str, strlen(opentag), strlen(closetag), false, sel);
		hideMenu('', 'win');
		hideMenu();
	};
}